﻿using CoachingOrganizer.Logic.Helpers;
using CoachingOrganizer.Logic.Logic.Tools.MoneyCalculation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoachingOrganizer.Logic.Models.Tools.MoneyCalculation
{
    public class ReportModel
    {
        private TimeSpan _currentReportPeriodSize = TimeSpan.FromDays(StableMoneyFlowItemModel.AsDaysInMonth);
        private PeriodReportModel _currentPeriodReport;
        private PeriodReportModel[] _periodReports;

        public TimeSpan CurrentReportPeriodSize
        {
            get { return _currentReportPeriodSize; }
            set
            {
                _currentReportPeriodSize = value;
                UpdatePeriodReports();
                OnCurrentReportPeriodSizeChanged();
            }
        }

        public PeriodReportModel CurrentPeriodReport
        {
            get { return _currentPeriodReport; }
            set
            {
                _currentPeriodReport = value;
                OnCurrentPeriodReportChanged();
            }
        }

        public IEnumerable<PeriodReportModel> PeriodReports
        {
            get { return _periodReports; }
        }

        public event EventHandler CurrentReportPeriodSizeChanged;

        public event EventHandler CurrentPeriodReportChanged;

        public event EventHandler PeriodReportsChanged;

        internal Func<IEnumerable<PeriodReportLogic>> EnumeratePeriodReportsDelegate;

        public ReportModel()
        {

        }

        private void UpdatePeriodReports()
        {
            _periodReports = EnumeratePeriodReportsDelegate().Select(l => l.Model).ToArray();
        }

        protected virtual void OnCurrentReportPeriodSizeChanged()
        {
            EventHandler handler = CurrentReportPeriodSizeChanged;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }

        protected virtual void OnCurrentPeriodReportChanged()
        {
            EventHandler handler = CurrentPeriodReportChanged;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }

        protected virtual void OnPeriodReportsChanged()
        {
            EventHandler handler = PeriodReportsChanged;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }
    }
}
